﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Economo.Services.DataContext
{
    public class BankAccountsDataContext : IDisposable 
    {
        DataClassesDataContext _context = new DataClassesDataContext(ConfigurationManager.ConnectionStrings["Economo"].ConnectionString);

        public List<BankAccount> GetBankAccounts()
        {
            try
            {
                var accounts = from a in _context.BankAccounts
                               orderby a.BankId, a.Id
                               select a;
                List<BankAccount> result = new List<BankAccount>(accounts);
                return result;
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageDataAccessException(new ExceptionHandling.DataAccessException("Error getting bank accounts", ex));
                return null;
            }
        }
        
        public List<BankAccount> GetBankAccounts(byte bankId)
        {
            try
            {
                var accounts = from a in _context.BankAccounts
                               where a.BankId == bankId
                               orderby a.Id
                               select a;
                List<BankAccount> result = new List<BankAccount>(accounts);
                return result;
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageDataAccessException(new ExceptionHandling.DataAccessException("Error getting bank accounts", ex));
                return null;
            }

        }


            
        public BankAccount GetAccountById(byte bankId, Int16 accountId)
        {
            try
            {
                BankAccount account = (from b in _context.BankAccounts
                             where b.BankId == bankId && b.Id == accountId
                                       select b).FirstOrDefault<BankAccount>();
                return account;

            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageDataAccessException(new ExceptionHandling.DataAccessException("Error getting bank account by id", ex));
                return null;
            }
        }
        public BankAccount GetAccountById(Int16 accountId)
        {
            try
            {
                BankAccount account = (from a in _context.BankAccounts
                                       where a.Id == accountId
                                       select a).FirstOrDefault<BankAccount>();
                return account;

            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageDataAccessException(new ExceptionHandling.DataAccessException("Error getting bank account by id", ex));
                return null;
            }
        }

        public Int16 Insert(BankAccount newBankAccount)
        {
            try
            {

                InsertBankAccountResult result = _context.InsertBankAccount(newBankAccount.BankId,newBankAccount.Description,newBankAccount.CurrencyId).FirstOrDefault<InsertBankAccountResult>();
                if (result.Id.HasValue)
                    return result.Id.Value;
                else
                    throw new ExceptionHandling.DataAccessException("Impossible to retrieve autogenerated id when inserting new row");
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageDataAccessException(new ExceptionHandling.DataAccessException("Error inserting new bank account", ex));
                return 0;
            }
        }

        public void Update(BankAccount account)
        {
            try
            {
                BankAccount updated = (from a in _context.BankAccounts
                                where a.BankId == account.BankId && a.Id == account.Id
                                select a).First();
                if (updated != null)
                {
                    updated.Description = account.Description;
                    updated.CurrencyId = account.CurrencyId;
                    _context.SubmitChanges(System.Data.Linq.ConflictMode.FailOnFirstConflict);
                }
                else
                    throw new ExceptionHandling.DataAccessException("BankAccount not found with Id=" + account.Id.ToString());
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageDataAccessException(new ExceptionHandling.DataAccessException("Error updating bank account", ex));
            }
        }
        public void Delete(BankAccount account)
        {
            try
            {
                _context.BankAccounts.DeleteOnSubmit(account);
                _context.SubmitChanges(System.Data.Linq.ConflictMode.FailOnFirstConflict);
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageDataAccessException(new ExceptionHandling.DataAccessException("Error deleting bank account", ex));
            }
        }
        public void Delete(byte bankId,Int16 accountId)
        {
            try
            {
                BankAccount account = GetAccountById(bankId,accountId);
                if (account != null)
                    Delete(account);
                else
                    throw new ExceptionHandling.DataAccessException("Bank account to delete not found");
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageDataAccessException(new ExceptionHandling.DataAccessException("Error deleting bank account", ex));
            }
        }



        public void Dispose()
        {
            _context.Dispose();
        }
    }
}
